package Queue;

import java.util.Arrays;

public class PriorityQueue<E> implements Queue<E> {
    private int capacity = 10;
    Object[] arr = new Object[capacity];
    private  int size;

    public PriorityQueue(int capacity) {
        this.capacity = capacity;
    }

    public PriorityQueue() {
    }

    @Override
    public boolean offer(E value) {
        if (isFull()) {
            throw new CapacityFull("The capacity is full");
        }
        arr[size] = value;
        size++;
        return true;
    }

    @Override
    public E poll() {
        if(isEmpty()) {
            System.out.println("该队列为空！");
            return null;
        }
        //根据优先级出队,默认按照数字大小
        if (size == 1) {
            size--;
            return (E)arr[0];
        }
        E temp = null;
        int i = 0;
        for (int k = 1; k < size; k++) {
            if ( (int)arr[k] > (int)arr[i]) {
                temp = (E)arr[k];
                i = k;
            }
        }
        for (int j = i; j < size; j++) {
            arr[j] = arr[j+1];
        }
        size--;
        return temp;
    }

    @Override
    public E peek() {
        if (isEmpty()){
            System.out.println("该队列为空！");
            return null;
        }
        //根据优先级出队,默认按照数字大小
        int i = 0;
        int j = 1;
        for (int k = 1; k < size; k++) {
            if ((int)arr[k] > (int)arr[i]) {
                i = k;
            }
        }
        return (E)arr[i];
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }
}
